package com.android.lovingpet.adopt.chat;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.bean.Account;
import com.android.lovingpet.bean.Team;
import com.android.lovingpet.util.Utility;
import com.bumptech.glide.Glide;

import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {

    private Context context;
    private List<Message> messages;

    public ChatAdapter(List<Message> messages) {
        this.messages = messages;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_chat, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Message message = messages.get(position);
        if (message.you == null) {
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.rightName.setText(message.me.getName());
            holder.rightMsg.setText(message.contend);
            Glide.with(context).load(message.me.getHead()).into(holder.rightHead);
        } else {
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.leftName.setText(message.you.getName());
            holder.leftMsg.setText(message.contend);
            Glide.with(context).load(message.you.getImage()).into(holder.leftHead);
        }
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout leftLayout;
        private RelativeLayout rightLayout;
        private TextView leftMsg;
        private TextView rightMsg;
        private ImageView leftHead;
        private ImageView rightHead;
        private TextView leftName;
        private TextView rightName;

        public ViewHolder(View itemView) {
            super(itemView);
            leftLayout = itemView.findViewById(R.id.left_layout);
            rightLayout = itemView.findViewById(R.id.right_layout);
            leftMsg = itemView.findViewById(R.id.left_msg);
            rightMsg = itemView.findViewById(R.id.right_msg);
            leftHead = itemView.findViewById(R.id.left_head);
            rightHead = itemView.findViewById(R.id.right_head);
            leftName = itemView.findViewById(R.id.left_name);
            rightName = itemView.findViewById(R.id.right_name);
        }
    }

    public static class Message {
        Account me;
        Team you;
        String contend;

        public Message(Account me, Team you, String contend) {
            this.me = me;
            this.you = you;
            this.contend = contend;
        }
    }
}
